﻿Jxstar.currentPage = function() {
	var config = {param:{},initpage:function(page, define){},eventcfg:{}};
	
	var Dataaudit = Jxstar.findComboData('audit');
	var Datactlprop = Jxstar.findComboData('ctlprop');
	var items = [{
		border:false,
		layout:'form',
		autoHeight:true,
		cls:'x-form-main',
		items:[{
			anchor:'100%',
			border:false,
			layout:'column',
			autoHeight:true,
			items:[{
				border:false,
				columnWidth:0.495,
				layout:'form',
				items:[
					{xtype:'textfield', fieldLabel:'单号', name:'store_in__in_code', readOnly:true, anchor:'100%', maxLength:20},
					{xtype:'datefield', fieldLabel:'入库日期', name:'store_in__in_date', defaultval:'fun_getToday()', format:'Y-m-d', allowBlank:false, labelStyle:'color:#0077FF;', labelSeparator:'*', anchor:'100%'},
					{xtype:'textfield', fieldLabel:'仓管员', name:'store_in__edit_user', defaultval:'fun_getUserName()', readOnly:true, anchor:'100%', maxLength:20},
					{xtype:'numberfield', renderer:JxUtil.formatMoney(2), fieldLabel:'入库金额', name:'store_in__in_money', anchor:'100%', maxLength:22},
					{xtype:'hidden', fieldLabel:'仓库ID', name:'store_in__house_id', anchor:'100%'}
				]
			},{
				border:false,
				columnWidth:0.495,
				layout:'form',
				items:[
					{xtype:'combo', fieldLabel:'状态', name:'store_in__auditing',
						anchor:'100%', readOnly:true, editable:false,
						store: new Ext.data.SimpleStore({
							fields:['value','text'],
							data: Dataaudit
						}),
						emptyText: jx.star.select,
						mode: 'local',
						triggerAction: 'all',
						valueField: 'value',
						displayField: 'text',
						value: Dataaudit[0][0]},
					{xtype:'trigger', fieldLabel:'仓库', name:'store_in__house_name',
						anchor:'100%', triggerClass:'x-form-search-trigger',
						maxLength:50, allowBlank:false, labelStyle:'color:#0077FF;', labelSeparator:'*', editable:true,
						onTriggerClick: function() {
							if (this.menu == null) {
								var selcfg = {"pageType":"combogrid", "nodeId":"store_house", "layoutPage":"/public/layout/layout_tree.js", "sourceField":"store_house.house_name", "targetField":"store_in.house_name", "whereSql":"", "whereValue":"", "whereType":"", "isSame":"1", "isShowData":"1", "isMoreSelect":"0","isReadonly":"0","queryField":"store_house.house_name","likeType":"all","fieldName":"store_in.house_name"};
								this.menu = Jxstar.createComboMenu(this);
								JxSelect.createComboGrid(selcfg, this.menu, 'node_store_in_form');
							}
							this.menu.show(this.el);
						}},
					{xtype:'combo', fieldLabel:'送货人', name:'store_in__send_user',
						anchor:'100%', triggerClass:'x-form-search-trigger',
						maxLength:20, allowBlank:false, labelStyle:'color:#0077FF;', labelSeparator:'*', editable:true,
						listeners:{afterrender: function(combo) {
							JxSelect.initCombo('store_in', combo, 'node_store_in_form');
						}}},
					{xtype:'checkboxgroup', fieldLabel:'制单员', name:'store_in__pur_user', defaultval:'1', 
						anchor:'100%', editable:false,
						items:JxUtil.getGroupData('ctlprop')},
					{xtype:'hidden', fieldLabel:'主键', name:'store_in__in_id', anchor:'100%'},
					{xtype:'hidden', fieldLabel:'仓管员ID', name:'store_in__edit_userid', defaultval:'fun_getUserId()', anchor:'100%'}
				]
			}
			]
		},{
			anchor:'100%',
			border:false,
			layout:'column',
			autoHeight:true,
			items:[{
				border:false,
				columnWidth:0.99,
				layout:'form',
				items:[
					{xtype:'textarea', fieldLabel:'入库说明', name:'store_in__in_desc', allowBlank:false, labelStyle:'color:#0077FF;', labelSeparator:'*', anchor:'100%', height:90, maxLength:200}
				]
			}
			]
		}]
	}];
	
	config.param = {
		items: items,
		funid: 'store_in'
	};

	config.param.formWidth = '100%';
	JxFormSub.formAddFile(config);
	JxFormSub.formAddTab(config);

	config.initpage = function(formNode){
	var event = formNode.event;
	
	//检查子表数据
	event.on('initother', function(event) {
		var form = event.form;
		var define = event.define;
		var pkvalue = form.get(define.pkcol);
        
		JxUtil.delay(1000, function(){
            var items = formNode.page.items.items;
            for (var i = 0; i < items.length; i++) {
                var data = items[i].data;
                if ('store_indet' == data) {
                    if (Ext.isEmpty(pkvalue)) {
                        //JxHint.alert('['+define.nodeid+']没有主键值，不能加载子表数据！');
                        return;
                    }
                    var subgrid = items[i].getComponent(0);//子表表格对象
                    var store = subgrid.getStore();
                    //如果没有加载数据则重新加载一次
                    if (store.getCount() == 0) {
                        Jxstar.loadSubData(subgrid, pkvalue);
                        console.log('---------------loadSubData.pkvalue'+pkvalue);
                    }
                }
            }
        });
	});
	
};

config.eventcfg={
	copycreate : function(){
		var self = this;
        var form = self.page.getForm();//表单对象
        var myGrid = form.myGrid;//主表表格对象
        var keyid = 'jxstar-986-5101';//主键
        
		//在表格中添加一条记录
		var insertData = function(record) {
			var store = myGrid.getStore();
			store.insert(0, record);
			myGrid.getSelectionModel().selectFirstRow();
		};
      
		//加载显示数据
		var options = {
			where_sql: 'store_in.in_id = ?',
			where_type: 'string',
			where_value: keyid,
			callback: function(data) { 
				//如果没有数据则执行新增
				if (data.length == 0) {
				  self.page.formNode.event.create();
				} else {
				 var r = self.page.formNode.event.newRecord(data[0]);
				 self.page.getForm().myRecord = r;
				 self.page.getForm().loadRecord(r);
				 //不刷新表格，直接在表格中新增一条记录
				 insertData(r);
				}
			}
		};
		Jxstar.queryData(self.define.nodeid, options);
        
 		//重新加载子表数据
		    var items = self.page.items.items;
		    for(var i=0; i<items.length; i++){
			   var data = items[i].data
               if ('store_indet' == data || 'store_indet1' == data) {
				  var subgrid = items[i].getComponent(0);//子表表格对象
                  Jxstar.loadSubData(subgrid, keyid);
               }
		    }
	}
};
	
	return new Jxstar.FormNode(config);
}